package com.onjob.message;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import util.dao.HibernateDaoImpl;

public class MessageDaoImpl extends HibernateDaoImpl<Message,String> implements
		MessageDao {

	public void setReadTag(final String messageId){
		StringBuffer hql = new StringBuffer();
		hql.append("update Message m set m.visible=1 where 1=1 ");
		hql.append("and m.id=:messageId ");
		final String s = new String(hql);
		this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(s);
						query.setString("messageId", messageId);
						query.executeUpdate();
						return null;
					}
					
				}
		);
	}
	
	@SuppressWarnings("unchecked")
	public List<Message> hasNotReadMessage(final String userId){
		StringBuffer hql = new StringBuffer();
		hql.append("from Message m where 1=1 ");
		hql.append("and m.visible=0 ");
		hql.append("and m.resumeRecruitId.resumeId.personInfo.userId.id=:userId ");
		
		final String s = new String(hql);
		List<Message> list = (List<Message>)this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
							Query query = session.createQuery(s);
							query.setString("userId", userId);
						return (List<Message>)(query.list());
					}
					
				}
		);
		return list;
	}
}
